using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.IO;
using System.Text.RegularExpressions;

namespace NET.Library.Comm
{
    /// <summary>
    /// Class Name : Email
    /// Author     : XYZ Web Solutions
    /// Synopsis   : Provides Email related functionality.
    /// </summary>
    public class Email
    {
        #region Private Variables
        /// <summary>
        /// Used for SMTP Server Name property.
        /// </summary>
        protected static string _smtpServer = "";
        /// <summary>
        /// Used for SMTP Server Port property.
        /// </summary>
        protected static int _smtpServerPort = 25;
        /// <summary>
        /// Used for SMTP Send Using property.
        /// </summary>
        protected static int _smtpSendUsing = 2;
        /// <summary>
        /// Used for SMTP Server User property.
        /// </summary>
        protected static string _smtpUser = "";
        /// <summary>
        /// Used for SMTP Server Password property.
        /// </summary>
        protected static string _smtpPassword = "";
        /// <summary>
        /// Used for SMTP Server Authenticate property.
        /// </summary>
        protected static int _smtpAuthenticate = 1;

        private static bool _UseAsync = false;
        #endregion

        #region Properties

        /// <summary>
        /// Gets/Sets the server name or IP Address of the mail server to be used.
        /// </summary>
        /// <value>ServerName accesses the value of the m_serverName data member</value>
        public static string SmtpServer
        {
            get { return _smtpServer; }
            set { _smtpServer = value; }
        }

        /// <summary>
        /// Gets/Sets the user name to be used when logging in to
        /// a smtp email server.
        /// </summary>
        public static string UserName
        {
            get { return _smtpUser; }
            set { _smtpUser = value; }
        }

        /// <summary>
        /// Gets/Sets the user name to be used when logging in to
        /// a smtp email server.
        /// </summary>
        public static string Password
        {
            get { return _smtpPassword; }
            set { _smtpPassword = value; }
        }

        /// <summary>
        /// Gets/Sets the server port to be used when logging in to
        /// an smtp email server.
        /// </summary>
        public static int ServerPort
        {
            get { return _smtpServerPort; }
            set { _smtpServerPort = value; }
        }

        /// <summary>
        /// Gets/Sets the transport mechanism when sending data to
        /// an smtp email server. (Integer constant)
        /// </summary>
        public static int SendUsing
        {
            get { return _smtpSendUsing; }
            set { _smtpSendUsing = value; }
        }

        /// <summary>
        /// Gets/Sets the authentication value when sending data to
        /// an smtp email server. (Integer constant)
        /// </summary>
        public static int Authenticate
        {
            get { return _smtpAuthenticate; }
            set { _smtpAuthenticate = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public static bool UseAsync
        {
            get { return _UseAsync; }
            set { _UseAsync = value; }
        }
        #endregion


        /// <summary>
        /// *********************************************************************************************************************************
        /// * Name        : SendMail 
        /// * Description : Send email to specified recipient(s) 
        /// * Syntax      : SendMail(ToEmail, FromEmail, MessageSubject, MessageBody)
        /// *********************************************************************************************************************************
        /// </summary>
        /// <paramref name="ToEmail"/>
        /// <param name="ToEmail">A semicolon-delimited list of recipient e-mail addresses.</param>
        /// <param name="FromEmail">Sender's e-mail address.</param>
        /// <param name="MessageSubject">The subject line of the e-mail message.</param>
        /// <param name="MessageBody">The body of the e-mail message.</param>
        /// <returns>Returns true if all recipient addresses are valid or false if not.</returns>
        /// <remarks>Sends email to all recipients.</remarks>
        /// <example>bool blnMailSent = SendMail("test@firstchoice.org.uk", "support@firstchoice.org.uk", "Subject", 
        /// "Message")</example>
        public static bool SendMail(ref MailAddress ToEmail, MailAddress FromEmail, string MessageSubject, string MessageBody)
        {
            return MailSender(ref ToEmail, FromEmail, null, null, MessageSubject, MessageBody, false, MailPriority.Normal, null);
        }

        /// <summary>
        /// *********************************************************************************************************************************
        /// * Name        : SendMail 
        /// * Description : Send email to specified recipient(s) 
        /// * Syntax      : Resp = SendMail(ToEmail, FromEmail, MessageSubject, MessageBody, MailFormat)
        /// *********************************************************************************************************************************
        /// </summary>
        /// <paramref name="ToEmail"/>
        /// <param name="ToEmail">A semicolon-delimited list of recipient e-mail addresses.</param>
        /// <param name="FromEmail">Sender's e-mail address.</param>
        /// <param name="CCTo">A semicolon-delimited list of carbon copy recipient e-mail addresses.</param>
        /// <param name="MessageSubject">The subject line of the e-mail message.</param>
        /// <param name="MessageBody">The body of the e-mail message.</param>
        /// <returns>Returns true if all recipient addresses are valid or false if not.</returns>
        /// <remarks></remarks>
        /// <example>bool blnMailSent = SendMail("test@firstchoice.org.uk", "support@firstchoice.org.uk", 
        /// "test2@firstchoice.org.uk", "Subject", "Message")</example>
        public static bool SendMail(ref MailAddress ToEmail, MailAddress FromEmail, MailAddress CCTo, string MessageSubject, string MessageBody)
        {
            return MailSender(ref ToEmail, FromEmail, CCTo, null, MessageSubject, MessageBody, false, MailPriority.Normal, null);
        }

        /// <summary>
        /// *********************************************************************************************************************************
        /// * Name        : SendMail 
        /// * Description : Send email to specified recipient(s) 
        /// * Syntax      : Resp = SendMail(ToEmail, FromEmail, MessageSubject, MessageBody, MailFormat)
        /// *********************************************************************************************************************************
        /// </summary>
        /// <paramref name="ToEmail"/>
        /// <param name="ToEmail">A semicolon-delimited list of recipient e-mail addresses.</param>
        /// <param name="FromEmail">Sender's e-mail address.</param>
        /// <param name="CCTo">A semicolon-delimited list of carbon copy recipient e-mail addresses.</param>
        /// <param name="BCCTo">A semicolon-delimited list of blind carbon copy recipient e-mail addresses.</param>
        /// <param name="MessageSubject">The subject line of the e-mail message.</param>
        /// <param name="MessageBody">The body of the e-mail message.</param>
        /// <returns>Returns true if all recipient addresses are valid or false if not.</returns>
        /// <remarks></remarks>
        /// <example>bool blnMailSent = SendMail("test@firstchoice.org.uk", "support@firstchoice.org.uk", 
        /// "test2@firstchoice.org.uk", "test3@firstchoice.org.uk", "Subject", "Message")</example>
        public static bool SendMail(ref MailAddress ToEmail, MailAddress FromEmail, MailAddress CCTo, 
            MailAddress BCCTo, string MessageSubject, string MessageBody)
        {
            return MailSender(ref ToEmail, FromEmail, CCTo, BCCTo, MessageSubject, MessageBody, false, MailPriority.Normal, null);
        }

        /// <summary>
        /// *********************************************************************************************************************************
        /// * Name        : SendMail 
        /// * Description : Send email to specified recipient(s) 
        /// * Syntax      : Resp = SendMail(ToEmail, FromEmail, MessageSubject, MessageBody, MailFormat)
        /// *********************************************************************************************************************************
        /// </summary>
        /// <paramref name="ToEmail"/>
        /// <param name="ToEmail">A semicolon-delimited list of recipient e-mail addresses.</param>
        /// <param name="FromEmail">Sender's e-mail address.</param>
        /// <param name="CCTo">A semicolon-delimited list of carbon copy recipient e-mail addresses.</param>
        /// <param name="BCCTo">A semicolon-delimited list of blind carbon copy recipient e-mail addresses.</param>
        /// <param name="MessageSubject">The subject line of the e-mail message.</param>
        /// <param name="MessageBody">The body of the e-mail message.</param>
        /// <param name="IsBodyHtml">Specifies if the e-mail format should be HTML.</param>
        /// <returns>Returns true if all recipient addresses are valid or false if not.</returns>
        /// <remarks></remarks>
        /// <example>bool blnMailSent = SendMail("test@firstchoice.org.uk", "support@firstchoice.org.uk", 
        /// "test2@firstchoice.org.uk", "test3@firstchoice.org.uk", "Subject", "Message", MailFormat.Html)</example>
        public static bool SendMail(ref MailAddress ToEmail, MailAddress FromEmail, MailAddress CCTo, 
            MailAddress BCCTo, string MessageSubject, string MessageBody,
            bool IsBodyHtml)
        {
            return MailSender(ref ToEmail, FromEmail, CCTo, BCCTo, MessageSubject, MessageBody, IsBodyHtml, MailPriority.Normal, null);
        }

        /// <summary>
        /// *********************************************************************************************************************************
        /// * Name        : SendMail 
        /// * Description : Send email to specified recipient(s) 
        /// * Syntax      : Resp = SendMail(ToEmail, FromEmail, MessageSubject, MessageBody, MailFormat)
        /// *********************************************************************************************************************************
        /// </summary>
        /// <paramref name="ToEmail"/>
        /// <param name="ToEmail">A semicolon-delimited list of recipient e-mail addresses.</param>
        /// <param name="FromEmail">Sender's e-mail address.</param>
        /// <param name="CCTo">A semicolon-delimited list of carbon copy recipient e-mail addresses.</param>
        /// <param name="BCCTo">A semicolon-delimited list of blind carbon copy recipient e-mail addresses.</param>
        /// <param name="MessageSubject">The subject line of the e-mail message.</param>
        /// <param name="MessageBody">The body of the e-mail message.</param>
        /// <param name="MailPriority">Specifies the priority level for the e-mail message.</param>
        /// <returns>Returns true if all recipient addresses are valid or false if not.</returns>
        /// <remarks></remarks>
        /// <example>bool blnMailSent = SendMail("test@firstchoice.org.uk", "support@firstchoice.org.uk", 
        /// "test2@firstchoice.org.uk", "test3@firstchoice.org.uk", "Subject", "Message", MailPriority.Normal)</example>
        public static bool SendMail(ref MailAddress ToEmail, MailAddress FromEmail, MailAddress CCTo, 
            MailAddress BCCTo, string MessageSubject, string MessageBody,
                                    MailPriority MailPriority)
        {
            return MailSender(ref ToEmail, FromEmail, CCTo, BCCTo, MessageSubject, MessageBody, false, MailPriority, null);
        }
        /// <summary>
        /// *********************************************************************************************************************************
        /// * Name        : SendMail 
        /// * Description : Send email to specified recipient(s) 
        /// * Syntax      : Resp = SendMail(ToEmail, FromEmail, MessageSubject, MessageBody, MailFormat)
        /// *********************************************************************************************************************************
        /// </summary>
        /// <paramref name="ToEmail"/>
        /// <param name="ToEmail">A semicolon-delimited list of recipient e-mail addresses.</param>
        /// <param name="FromEmail">Sender's e-mail address.</param>
        /// <param name="MessageSubject">The subject line of the e-mail message.</param>
        /// <param name="MessageBody">The body of the e-mail message.</param>
        /// <param name="Attachment">Specifies the attachment that is transmitted with the message.</param>
        /// <returns>Returns true if all recipient addresses are valid or false if not.</returns>
        /// <remarks></remarks>
        /// <example>bool blnMailSent = SendMail("test@firstchoice.org.uk", "support@firstchoice.org.uk", "Subject", 
        /// "Message", FileInfo("c:\test.txt"))</example>
        public static bool SendMail(ref MailAddress ToEmail, MailAddress FromEmail, string MessageSubject, 
            string MessageBody, FileInfo Attachment)
        {
            return MailSender(ref ToEmail, FromEmail, null, null, MessageSubject, MessageBody, false, MailPriority.Normal, Attachment);
        }
        /// <summary>
        /// *********************************************************************************************************************************
        /// * Name        : SendMail 
        /// * Description : Send email to specified recipient(s) 
        /// * Syntax      : Resp = SendMail(ToEmail, FromEmail, MessageSubject, MessageBody, MailFormat)
        /// *********************************************************************************************************************************
        /// </summary>
        /// <paramref name="ToEmail"/>
        /// <param name="ToEmail">A semicolon-delimited list of recipient e-mail addresses.</param>
        /// <param name="FromEmail">Sender's e-mail address.</param>
        /// <param name="CCTo">A semicolon-delimited list of carbon copy recipient e-mail addresses.</param>
        /// <param name="BCCTo">A semicolon-delimited list of blind carbon copy recipient e-mail addresses.</param>
        /// <param name="MessageSubject">The subject line of the e-mail message.</param>
        /// <param name="MessageBody">The body of the e-mail message.</param>
        /// <param name="IsBodyHtml">Specifies if the e-mail format should be HTML.</param>
        /// <param name="MailPriority">Specifies the priority level for the e-mail message.</param>
        /// <returns>Returns true if all recipient addresses are valid or false if not.</returns>
        /// <remarks></remarks>
        /// <example>bool blnMailSent = SendMail("test@firstchoice.org.uk", "support@firstchoice.org.uk", 
        /// "test2@firstchoice.org.uk", "test3@firstchoice.org.uk", "Subject", 
        /// "Message", MailFormat.Html, MailPriority.Normal)</example>
        public static bool SendMail(ref MailAddress ToEmail, MailAddress FromEmail, MailAddress CCTo, MailAddress BCCTo, 
            string MessageSubject, string MessageBody,
           bool IsBodyHtml, MailPriority MailPriority)
        {
            return MailSender(ref ToEmail, FromEmail, CCTo, BCCTo, MessageSubject, MessageBody, IsBodyHtml, MailPriority, null);
        }
        /// <summary>
        /// *********************************************************************************************************************************
        /// * Name        : SendMail 
        /// * Description : Send email to specified recipient(s) 
        /// * Syntax      : Resp = SendMail(ToEmail, FromEmail, CCTo, BCCTo, MessageSubject, MessageBody, MailFormat, MailPriority, Attachment)
        /// *********************************************************************************************************************************
        /// </summary>
        /// <paramref name="ToEmail"/>
        /// <param name="ToEmail">A semicolon-delimited list of recipient e-mail addresses.</param>
        /// <param name="FromEmail">Sender's e-mail address.</param>
        /// <param name="CCTo">A semicolon-delimited list of carbon copy recipient e-mail addresses.</param>
        /// <param name="BCCTo">A semicolon-delimited list of blind carbon copy recipient e-mail addresses.</param>
        /// <param name="MessageSubject">The subject line of the e-mail message.</param>
        /// <param name="MessageBody">The body of the e-mail message.</param>
        /// <param name="IsBodyHtml">Specifies if the e-mail format should be HTML.</param>
        /// <param name="MailPriority">Specifies the priority level for the e-mail message.</param>
        /// <param name="Attachment">Specifies the attachment that is transmitted with the message.</param>
        /// <returns>Returns true if all recipient addresses are valid or false if not.</returns>
        /// <remarks></remarks>
        /// <example>bool blnMailSent = SendMail("test@firstchoice.org.uk", "support@firstchoice.org.uk", 
        /// "test2@firstchoice.org.uk", "test3@firstchoice.org.uk", "Subject", 
        /// "Message", MailFormat.Html, MailPriority.Normal, FileInfo("c:\test.txt"))</example>
        public static bool SendMail(ref MailAddress ToEmail, MailAddress FromEmail, MailAddress CCTo, 
            MailAddress BCCTo, string MessageSubject, string MessageBody,
           bool IsBodyHtml, MailPriority MailPriority, FileInfo Attachment)
        {
            return MailSender(ref ToEmail, FromEmail, CCTo, BCCTo, MessageSubject, MessageBody, IsBodyHtml, MailPriority, Attachment);
        }
        /// <summary>
        /// *********************************************************************************************************************************
        /// * Name        : SendMail 
        /// * Description : Send email to specified recipient(s) 
        /// * Syntax      : Resp = SendMail(ToEmail, FromEmail, MessageSubject, MessageBody, MailFormat)
        /// *********************************************************************************************************************************
        /// </summary>
        /// <paramref name="ToEmail"/>
        /// <param name="ToEmail">A semicolon-delimited list of recipient e-mail addresses.</param>
        /// <param name="FromEmail">Sender's e-mail address.</param>
        /// <param name="MessageSubject">The subject line of the e-mail message.</param>
        /// <param name="MessageBody">The body of the e-mail message.</param>
        /// <param name="IsBodyHtml">Specifies if the e-mail format should be HTML.</param>
        /// <returns>Returns true if all recipient addresses are valid or false if not.</returns>
        /// <remarks>Sends email to all recipients if they are all valid and returns true. If any of the recipients have invalid addresses, 
        /// no email is sent, the invalid recipients are assigned to the ToEmail parameter and returns false.</remarks>
        /// <example>bool blnMailSent = SendMail("test@firstchoice.org.uk", "support@firstchoice.org.uk", "Subject", 
        /// "Message", MailFormat.Html)</example>
        public static bool SendMail(ref MailAddress ToEmail, MailAddress FromEmail, string MessageSubject, string MessageBody, 
            bool IsBodyHtml)
        {
            //int i = 0;
            //string[] strInvalidRecipients = new string[0];

            try
            {
                ///// Split ToEmail string into array
                //string[] strRecipients = ToEmail.Split((char)59); //Splits by ; character.

                //foreach (string strRecipient in strRecipients)
                //{
                //    //				Recipients colRecipients = new Recipients();
                //    //				colRecipients.Add(new Recipient(strRecipient));

                //    if (!EmailValid(strRecipient))
                //    {
                //        //Log invalid email address
                //        strInvalidRecipients.SetValue(strRecipient, i);
                //        //Iterate error counter
                //        i += 1;
                //    }
                //}

                //if (i == 0) //No invalid email addresses
                //{
                    //Send email to all recipients
                    MailSender(ref ToEmail, FromEmail, null, null, MessageSubject, MessageBody, IsBodyHtml, MailPriority.Normal, null);
                    return true;
                //}
                //else
                //{
                //    //if (strInvalidRecipients.Length > 0)
                //    //Maybe the vaild ones can be sent the email - will leave out for now.
                //    ToEmail = InvalidRecipients(strInvalidRecipients);
                //    return false;
                //}
            }
            finally
            {
                //strInvalidRecipients = null;
            }


        }





        //		private string SmtpServer()
        //		{
        //			if (ServerName.Length > 0) //use specified smtp server
        //			{
        //				return ServerName;
        //			}
        //			else //use default smtp server
        //			{
        //				return ConfigurationSettings.AppSettings["SmtpServer"];
        //			}
        //		}

        //		/// <summary>
        //		/// 
        //		/// </summary>
        //		/// <returns></returns>
        //		private static bool Mailer()
        //		{
        //			bool blnMailer = false;
        //			
        //
        //			return blnMailer;
        //
        //		}

        //		/// <summary>
        //		/// 
        //		/// </summary>
        //		public class Recipients : CollectionBase
        //		{
        //			///Restricts to Recipient types, items that can be added to the collection.
        //			
        //			public void Add(Recipient Item)
        //			{
        //				//Invokes Add method of the List object to add a string.
        //				List.Add(Item);
        //			}
        //			
        //			/// 
        //			
        //			/// <param name="index"></param>
        //			public void Remove(int index)
        //			{
        //				List.RemoveAt(index); 
        //			}
        //			
        //			
        //			/// <summary>
        //			/// This line declares the Item property as ReadOnly, and 
        //			/// declares that it will return a string object.
        //			/// </summary>
        //			/// <param name="Index"></param>
        //			/// <returns></returns>
        //			public Recipient Item(int Index)
        //			{
        //				///The appropriate item is retrieved from the List object and 
        //				///explicitly cast to the Recipient type, then returned to the 
        //				///caller.
        //				return (Recipient)List[Index];
        //			}
        //			
        //		}
        //		
        //		
        //		/// <summary>
        //		/// 
        //		/// </summary>
        //		public class Recipient
        //		{
        //			
        //			/// <summary>
        //			/// 
        //			/// </summary>
        //			public string Name;
        //			
        //			/// <summary>
        //			/// 
        //			/// </summary>
        //			public Recipient()
        //			{
        //			}
        //			/// <summary>
        //			/// 
        //			/// </summary>
        //			/// <param name="Item"></param>
        //			public Recipient(string Item)
        //			{
        //			}
        //		}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ToEmail"></param>
        /// <param name="FromEmail"></param>
        /// <param name="CCTo"></param>
        /// <param name="BCCTo"></param>
        /// <param name="MessageSubject"></param>
        /// <param name="MessageBody"></param>
        /// <param name="IsBodyHtml"></param>
        /// <param name="MailPriority"></param>
        /// <param name="Attachment"></param>
        internal static  bool MailSender(ref MailAddress ToEmail, MailAddress FromEmail, MailAddress CCTo,
            MailAddress BCCTo, string MessageSubject, string MessageBody, bool IsBodyHtml,
            MailPriority MailPriority, FileInfo Attachment)
        {

            if (UseAsync)
            {
                return AsyncEmail.MailSender(ref  ToEmail, FromEmail, CCTo,
                                             BCCTo, MessageSubject, MessageBody, IsBodyHtml,
                                            MailPriority, Attachment);
            }
            else
            {

                //int i = 0;
                MailMessage cdoMsg = new MailMessage();
                //string[] strInvalidRecipients = new string[0];

                try
                {
                    ///// Split ToEmail string into array
                    //string[] strRecipients = ToEmail.Split((char)59); //Splits by ; character.

                    //foreach (string strRecipient in strRecipients)
                    //{
                    //    //				Recipients colRecipients = new Recipients();
                    //    //				colRecipients.Add(new Recipient(strRecipient));

                    //    if (!EmailValid(strRecipient))
                    //    {
                    //        //Store invalid email address
                    //        strInvalidRecipients.SetValue(strRecipient, i);
                    //        //Iterate error counter
                    //        i += 1;
                    //    }
                    //}

                    //if (i == 0) //No invalid email addresses
                    //{
                    //Send email to all recipients
                    cdoMsg.To.Add(ToEmail);
                    cdoMsg.From = FromEmail;
                    if (CCTo != null)
                    {
                        cdoMsg.CC.Add(CCTo);
                    }
                    if (BCCTo != null)
                    {
                        cdoMsg.Bcc.Add(BCCTo);
                    }
                    cdoMsg.Subject = MessageSubject;
                    cdoMsg.Body = MessageBody;

                    cdoMsg.IsBodyHtml = IsBodyHtml;

                    if (MailPriority.ToString() == "")
                    {
                        MailPriority = MailPriority.Normal;
                    }
                    cdoMsg.Priority = MailPriority;
                    //if (Attachment.Length > 0)
                    if (Attachment != null)
                    {
                        if (Attachment.Exists)
                        {
                            cdoMsg.Attachments.Add(new Attachment(Attachment.FullName));
                            //cdoMsg.Attachments.Add(new MailAttachment(Attachment.FullName));
                        }
                        //else log file error?
                    }
                    //SmtpMail.SmtpServer = SmtpServer;

                    SmtpClient client = new SmtpClient();
                    if (!string.IsNullOrEmpty(SmtpServer))
                    {
                        client.Host = SmtpServer;
                    }

                    //SmtpMail.Send(cdoMsg);
                    client.Send(cdoMsg);
                    //if (Attachment != null)
                    //{
                    //    if (Attachment.Exists)
                    //    {
                    //        Attachment.Delete();
                    //    }
                    //}
                    return true;
                    //}
                    //else
                    //{
                    //    //Check if all recipients are invalid
                    //    if (i == strRecipients.Length)
                    //    {
                    //        ToEmail = InvalidRecipients(strInvalidRecipients);
                    //        return false;
                    //    }
                    //    //Maybe the vaild ones can be sent the email - will leave out for now.
                    //    ToEmail = InvalidRecipients(strInvalidRecipients);
                    //    return false;
                    //}
                }
                finally
                {
                    cdoMsg = null;
                    //strInvalidRecipients = null;
                }
            }
        }
        /// <summary>
        /// *********************************************************************************************************************************
        /// * Name        : EmailValid method
        /// * Description : Returns false if the EmailAddress
        /// *             : is invalid, otherwise returns true.
        /// * Syntax      : Resp = EmailValid(EmailAddress)
        /// *********************************************************************************************************************************
        /// <param name="EmailAddress">Email address to validate</param>
        /// <returns>Returns true or false.</returns>
        /// <remarks></remarks>
        /// <example>bool blnIsEmailValid = EmailValid("test@firstchoice.org.uk");</example>
        /// </summary>
        public static bool EmailValid(string EmailAddress)
        {
            //Regular expression
            string strPattern = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
            Match m;

            int i = 0;

            if (EmailAddress.Length.Equals(i))
            {
                return false;
            }

            m = Regex.Match(EmailAddress, strPattern);

            if (!m.Success) //Email address not valid
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Recipients"></param>
        /// <returns></returns>
        private static string InvalidRecipients(string[] Recipients)
        {
            string strInvalidRecipients = "";
            foreach (string strInvalidRecipient in Recipients)
            {
                strInvalidRecipients += strInvalidRecipient + ";";
            }
            return strInvalidRecipients;
        }
    }
}
